package com.wyg.service.impl;

import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.wyg.dao.UserDao;
import com.wyg.dao.impl.UserDaoImpl;
import com.wyg.pojo.User;
import com.wyg.service.ZeroService;

public class ZeroServiceImpl implements ZeroService{
	private HttpServletRequest request;
	private HttpServletResponse response;
	private UserDao udi;
	
	public UserDao getUdi() {
		return udi;
	}

	public void setUdi(UserDao udi) {
		this.udi = udi;
	}

	private Map<String, User> useMap;
	private HttpSession session;
	private ServletContext sc;
	
	@SuppressWarnings("unchecked")
	@Override
	public String login(String phone, String password) {
		
		User user = udi.login(phone, password);
		this.request = ServletActionContext.getRequest();
		if(user != null){
			request.getSession().setAttribute("user", user);
			if(phone.equals("admin")||phone=="admin"){
				return "manager";
			}else{
				session=ServletActionContext.getRequest().getSession();
				sc=ServletActionContext.getServletContext();
				useMap=(Map<String,User>) sc.getAttribute("user");
				if(useMap!=null){
					useMap.put(session.getId(),user);
				}
				return "user";
			}
		}else{
			return "input";
		}
		
	}

	@Override
	public String regist(User user) {
		udi.regist(user);
		return "success";
	}

	
}
